#include "stdafx.h"
#include "BookHelper.h"

namespace Librarian
{
	namespace Data
	{
		BookEntity^ BookHelper::ReadBook(DataRow^ row)
		{
			BookEntity^ entity = gcnew BookEntity();
			entity->BookNo = Convert::ToString(row["book_no"]);
			entity->Category = Convert::ToString(row["category"]);
			entity->Title = Convert::ToString(row["title"]);
			entity->Press = Convert::ToString(row["press"]);
			entity->PublishYear = Convert::ToInt32(row["publish_year"]);
			entity->Author = Convert::ToString(row["author"]);
			entity->Price = Convert::ToDecimal(row["price"]);
			entity->Total = Convert::ToInt32(row["total"]);
			entity->Stock = Convert::ToInt32(row["stock"]);
			return entity;
		}
		BookEntity^ BookHelper::ReadBook(DataTable^ table)
		{
			if(table->Rows->Count == 0)
			{
				return gcnew BookEntity();
			}
			return ReadBook(table->Rows[0]);
		}
		
		array<BookEntity^>^ BookHelper::ReadBooks(DataTable^ table)
		{
			List<BookEntity^>^ entities = gcnew List<BookEntity^>();
			for(int i = 0; i < table->Rows->Count; i++)
			{
				DataRow^ row = table->Rows[i];
				entities->Add(ReadBook(row));                               /////////borrow_id
			}
			return entities->ToArray();

		}

		BorrowEntity^ BookHelper::BorrowHiss(DataTable^ table)
		{
			if(table->Rows->Count == 0)
			{
				return gcnew BorrowEntity();
			}
			return BorrowHis(table->Rows[0]);
		}
		BorrowEntity^ BookHelper::BorrowHis(DataRow^ row)
		{
			BorrowEntity^ entity = gcnew BorrowEntity();
			entity->BorrowId = Convert::ToInt32(row["borrow_id"]);
			entity->BookNo = Convert::ToString(row["book_no"]);
			entity->CardNo = Convert::ToString(row["card_no"]);
			entity->BorrowDate = Convert::ToDateTime(row["borrow_date"]);
			entity->ReturnDate = Convert::ToDateTime(row["return_date"]);
			entity->Handler = Convert::ToString(row["handler"]);
			return entity;
		}

	}
}